<?php include('menu.php'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="datepicker/css/smoothness/jquery-ui-1.7.2.custom.css">
<script type="text/javascript" src="datepicker/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="datepicker/js/jquery-ui-1.7.2.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    var dateBefore=null;
    $("#date_search").datepicker({
        dateFormat: 'dd-mm-yy',
        dayNamesMin: ['อา', 'จ', 'อ', 'พ', 'พฤ', 'ศ', 'ส'],
        monthNamesShort: ['มกราคม','กุมภาพันธ์','มีนาคม','เมษายน','พฤษภาคม','มิถุนายน','กรกฎาคม','สิงหาคม','กันยายน','ตุลาคม','พฤศจิกายน','ธันวาคม'],
        changeMonth: true,
        changeYear: true ,
        beforeShow:function(){
            if($(this).val()!=""){
                var arrayDate=$(this).val().split("-");
                arrayDate[2]=parseInt(arrayDate[2])-543;
                $(this).val(arrayDate[0]+"-"+arrayDate[1]+"-"+arrayDate[2]);
            }
            setTimeout(function(){
                $.each($(".ui-datepicker-year option"),function(j,k){
                    var textYear=parseInt($(".ui-datepicker-year option").eq(j).val())+543;
                    $(".ui-datepicker-year option").eq(j).text(textYear);
                });
            },50);

        },
        onChangeMonthYear: function(){
            setTimeout(function(){
                $.each($(".ui-datepicker-year option"),function(j,k){
                    var textYear=parseInt($(".ui-datepicker-year option").eq(j).val())+543;
                    $(".ui-datepicker-year option").eq(j).text(textYear);
                });
            },50);
        },
        onClose:function(){
            if($(this).val()!="" && $(this).val()==dateBefore){
                var arrayDate=dateBefore.split("-");
                arrayDate[2]=parseInt(arrayDate[2])+543;
                $(this).val(arrayDate[0]+"-"+arrayDate[1]+"-"+arrayDate[2]);
            }
        },
        onSelect: function(dateText, inst){
            dateBefore=$(this).val();
            var arrayDate=dateText.split("-");
            arrayDate[2]=parseInt(arrayDate[2])+543;
            $(this).val(arrayDate[0]+"-"+arrayDate[1]+"-"+arrayDate[2]);
        }

    });

});
</script>
</head>
<body>
<?php
//ลบข้อมูล
if($_GET["action"]=='del') {

	$Query = mysql_query("DELETE FROM buy_history WHERE buy_his_id = '".$_GET["id"]."'");
	$Query2 = mysql_query("DELETE FROM buy_detail WHERE buy_his_id = '".$_GET["id"]."'");

	
	header('Location: buy_manage.php');
}

//ค้นหาสินค้า
if($_POST["date_search"] != "" OR $_POST["bill_search"] != "" ) {
			$dmy = $_POST["date_search"];
			$d = substr($dmy, 0, 2);
			$m = substr($dmy, 3, 2);
			$y = substr($dmy, 6, 4);
			$date = ($y - 543) . '-' . $m . '-' . $d;
			
	$Query = "SELECT * FROM buy_history WHERE 1 ";
	
	if($_POST["date_search"] != "") {
		$Query .= "AND buy_his_date LIKE '%".$date."%' ";
	}
	if($_POST["bill_search"] != "") {
		$Query .= "AND buy_his_bill LIKE '%".$_POST["bill_search"]."%' ";
	}
	$Query .= "ORDER BY buy_his_date DESC";
} else {
	$Query = "SELECT * FROM buy_history ORDER BY buy_his_date DESC";
}
$objQuery = mysql_query($Query) or die ("Error Query [".$Query."]");
$total=mysql_num_rows($objQuery);
$e_page=50; // กำหนด จำนวนรายการที่แสดงในแต่ละหน้า   
if(!isset($_GET['s_page'])){   
	$_GET['s_page']=0;   
}else{   
	$chk_page=$_GET['s_page'];     
	$_GET['s_page']=$_GET['s_page']*$e_page;   
}   
$Query.=" LIMIT ".$_GET['s_page'].",$e_page";
$objQuery=mysql_query($Query);
if(mysql_num_rows($objQuery)>=1){   
	$plus_p=($chk_page*$e_page)+mysql_num_rows($objQuery);   
}else{   
	$plus_p=($chk_page*$e_page);       
}   
$total_p=ceil($total/$e_page);   
$before_p=($chk_page*$e_page)+1;  
?>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading"><h1>จัดการประวัติการซื้อ เฮือนคำ รีสอร์ท</h1><div class="buttons"><a onclick="JavaScript:window.location='buy_history.php'" class="button"><span>เพิ่มประวัติการซื้อ</span></a></div></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
            <td width="40%" class="center">วัน-เดือน-ปี ที่ซื้อ</td>
            <td width="40%" class="center">รหัสบิล</td>
            <td width="20%" class="center">จัดการ</td>
          </tr>
        </thead>
        <tbody>
		<form action="<?php $_SERVER["PHP_SELF"];?>" name="formsearch" method="post" id="formsearch">
          <tr class="filter">
            <td align="center"><input name="date_search" type="text" id="date_search" style="text-align: center;" /></td>
            <td align="center"><input name="bill_search" type="text" id="bill_search" style="text-align: center;" onkeydown="if(event.keyCode==13){formsearch.submit();}" /></td>
            <td align="center"><a onclick="formsearch.submit();" class="button"><span>ค้นหา</span></a></td>
          </tr>
		</form>
<?php
	$num=0;
	while($row = mysql_fetch_array($objQuery))
		{
			$num++;
?>
          <tr>
            <td class="left" style="padding-left:20px"><?php echo thai_date(strtotime($row["buy_his_date"]));?></td>
            <td class="left" style="padding-left:20px"><?php echo $row["buy_his_bill"];?></td>
 			<td class="center">[ <a href="buy_history_edit.php?id=<?php echo $row["buy_his_id"];?>">แก้ไข</a> ]  [ <a href="JavaScript:if(confirm('คุณต้องการยืนยันการลบข้อมูล ใช่หรือไม่?')==true){window.location='<?php echo $_SERVER["PHP_SELF"];?>?action=del&id=<?php echo $row["buy_his_id"];?>';}">ลบ</a> ]</td>
          </tr>
<?php
		} 
?>
        </tbody>
      </table>
<?php if($total>0){ ?>
<div class="browse_page" align="center">
 <?php
 // เรียกใช้งานฟังก์ชั่น สำหรับแสดงการแบ่งหน้า   
  page_navigator($before_p,$plus_p,$total,$total_p,$chk_page);    
  ?> 
</div>
<?php } ?>
  </div>
</div>

</body>
<?php mysql_close(); ?>
</html>
